package com.xy.modulemain.model

import com.blankj.utilcode.util.ToastUtils
import com.xy.baselibrary.base.MvvmBaseModel
import com.xy.baselibrary.base.MvvmNetworkObserver
import com.xy.modulemain.api.ApiService
import com.xy.modulemain.api.MainNetWorkApi
import com.xy.modulemain.bean.Item
import com.xy.modulemain.bean.PagingResult
import com.xy.modulemain.lisener.IBaseModelListener
import com.xy.network.bean.ResponseBean
import com.xy.network.observer.BaseObserver

/**
 * @ClassName ItemModel
 * @Author xy
 * @Date 2021/8/24 15:43
 * @Description 描述  请求接口  创建监听者将 信息回调到 View界面
 */
//class ItemModel(var loadListener: IBaseModelListener<ResponseBean<MutableList<Item>>>) {
open class ItemModel : MvvmBaseModel<ResponseBean<MutableList<Item>>> {

    constructor()
    constructor(itemId: String, itemName: String) : super(
        itemId,
        "",
        true,
        ResponseBean<MutableList<Item>>().javaClass
    )

    //请求请求接口
    override fun load() {
        MainNetWorkApi.getService(ApiService::class.java)
            ?.getList(pageNumber, 15)
            ?.compose(MainNetWorkApi.instance.applySchedulers(object :
                BaseObserver<ResponseBean<MutableList<Item>>>(this, this) {}))
    }

    override fun isPaging(): Boolean {
        return true
    }


    override fun onFailure(e: Throwable) {

        ToastUtils.showShort(e.message)
    }

    override fun onSuccess(t: ResponseBean<MutableList<Item>>, isFromCache: Boolean) {
        notifyResultToListener(t,t,isFromCache)
    }
}